﻿using Blogger.UI.Models;
using POC.Reports;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;


namespace Blogger.UI.Controllers
{
    public class QueryModel
    {
        public string BlobUrls { get; set; }
    }
    public class BlobController : FileApiController
    {
        public BlobController()
        {
            if (WebRequest.DefaultWebProxy.Credentials == null)
            {
                WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
            }
        }

        [HttpGet]
        [HttpPost]
        public IEnumerable BlobList()
        {
            var blobs = GetBloblist("root");
            return blobs;
        }
        [HttpGet]
        [HttpPost]
        public IEnumerable DeleteBlobs(QueryModel urls)
        {
            var blobs = DeleteBloblist("root", urls.BlobUrls);
            return blobs;
        }
        [HttpGet]
        [HttpPost]
        public HttpResponseMessage Azure()
        {
            var response = new List<ViewDataUploadFilesResult>();
            foreach (string file in HttpContext.Current.Request.Files)
            {
                var statuses = new List<ViewDataUploadFilesResult>();
                var wrapper = new HttpRequestWrapper(HttpContext.Current.Request);
                var headers = HttpContext.Current.Request.Headers;
                string clientFileName = headers[clientHeaderFile];
                string mimeType = FileLoaderExtn.GetMimeType(Path.GetFileName(file));

                if (string.IsNullOrEmpty(clientFileName))
                {
                    WriteBlob(wrapper, statuses);
                }
                else
                {
                    WritePartialBlob(clientFileName, wrapper, statuses);
                }
                var result = GetResponse(statuses, TextType);
                return result;
            }
            return GetResponse(response, TextType);
        }
        /*
        [HttpGet]
        [HttpPost]
        public HttpResponseMessage Upload()
        {  
            var response = new List<ViewDataUploadFilesResult>();
            foreach (string file in HttpContext.Current.Request.Files)
            {
                var statuses = new List<ViewDataUploadFilesResult>();
                var wrapper = new HttpRequestWrapper(HttpContext.Current.Request);
                var headers = HttpContext.Current.Request.Headers;
                string clientFileName = headers[clientHeaderFile];

                if (string.IsNullOrEmpty(clientFileName))
                {
                    UploadWholeFile(wrapper, statuses);
                }
                else
                {
                    UploadPartialFile(clientFileName, wrapper, statuses);
                }

                var result = GetResponse(statuses,TextType);
                return result;
            }
            return GetResponse(response, TextType);
        }

        [HttpGet]
        [HttpPost]
        public HttpResponseMessage UploadBasicFile()
        {
            // Get a reference to the file that our jQuery sent.  Even with multiple files, they will all be their own request and be the 0 index
            HttpPostedFile file = HttpContext.Current.Request.Files[0];
            // do something with the file in this space 
            // {....}
            // end of file doing

            // Now we need to wire up a response so that the calling script understands what happened
            HttpContext.Current.Response.ContentType = "text/plain";
            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            var result = new { name = file.FileName };

            HttpContext.Current.Response.Write(serializer.Serialize(result));
            HttpContext.Current.Response.StatusCode = 200;

            // For compatibility with IE's "done" event we need to return a result as well as setting the context.response
            return new HttpResponseMessage(HttpStatusCode.OK);
        }
         * */
    }
}
